package com.common.www.controller.global;

import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.global.GlobalConfig;
import com.common.www.controller.base.DefaultReadController;
import com.common.www.service.global.GlobalConfigService;
import com.common.www.data.global.GlobalConfigEnum;
import com.common.www.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import static com.common.www.data.global.GlobalConfigEnum.APP_ABOUT_US;
import static com.common.www.data.global.GlobalConfigEnum.APP_SERVICE_POLICY;
import static com.common.www.data.global.GlobalConfigEnum.APP_SERVICE_PROTOCOL;


@RestController
@CrossOrigin()
@Api(tags = "API 全局配置接口")
@RequestMapping(value = {"/config/"})
public class GlobalConfigController extends DefaultReadController<GlobalConfig, Long> {

    @Autowired
    GlobalConfigService globalConfigService;

    @Override
    protected IBaseService getService() {
        return globalConfigService;
    }

    @ApiOperation("存储配置测试接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "配置名称必须输入代码里配置的名称", defaultValue = ""),
    })
    @RequestMapping(value = "/saveTest", method = RequestMethod.POST)
    public Result saveTest(String name) {
        return R.ok(globalConfigService.getConfig(GlobalConfigEnum.getEnumByName(name)));
    }

    @ApiOperation("获取规则根据名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "配置名称必须输入代码里配置的名称", defaultValue = ""),
    })
    @RequestMapping(value = "/getConfig", method = RequestMethod.POST)
    public Result getConfig(String name) {
        String value = globalConfigService.getConfig(GlobalConfigEnum.getEnumByName(name));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        jsonObject.put("value", value);
        return R.ok(jsonObject);
    }

    @ApiOperation("设置规则根据名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "配置名称必须输入代码里配置的名称", defaultValue = ""),
            @ApiImplicitParam(name = "value", value = "值", defaultValue = ""),
    })
    @RequestMapping(value = "/setConfig", method = RequestMethod.POST)
    public Result setConfig(String name, String value) {
        return R.ok(globalConfigService.setConfig(GlobalConfigEnum.getEnumByName(name), value));
    }


//    @ApiOperation("获取隐私协议")
//    @ApiImplicitParams({
//    })
//    @RequestMapping(value = "/getPrivacyAgreement", method = RequestMethod.POST)
//    public Result getPrivacyAgreement(){
//        return R.ok(globalConfigService.getConfig(GlobalConfigEnum.PRIVACY_AGREEMENT));
//    }


    @ApiOperation("获取协议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "1.隐私政策 2.用户协议", defaultValue = ""),
    })
    @RequestMapping(value = "/getServiceProtocol", method = RequestMethod.POST)
    public Result getServiceProtocol(Integer type) {
        if (type == 1) {
            String value = globalConfigService.getConfig(APP_SERVICE_POLICY);
            return R.ok(value);
        } else if (type == 2) {
            String value = globalConfigService.getConfig(APP_SERVICE_PROTOCOL);
            return R.ok(value);
        } else if (type == 3) {
            String value = globalConfigService.getConfig(APP_ABOUT_US);
            return R.ok(value);
        }

        return R.ok();
    }
}
